package jfox.platform.organization.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;
import jfox.platform.organization.entity.Company;

/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Stateless
@Local
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = CompanyDAOBean.GET_COMPANY_BY_ID,
                query = "SELECT * FROM T_SYS_COMPANY WHERE ID=$ID AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                resultClass = Company.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),

        @NamedNativeQuery(
                name = CompanyDAOBean.GET_ALL_COMPANYS,
                query = "SELECT * FROM T_SYS_COMPANY WHERE VSTATUS=" + EntityObject.VSTATUS_NORMAL + " ORDER BY ID",
                resultClass = Company.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
                name = CompanyDAOBean.INSERT_COMPANY,
                query = "INSERT INTO T_SYS_COMPANY(" +
                        "ID," +
                        "NAME, " +
                        //"LOCAL_NAME, " +
                        "PRIORITY, " +
                        //"CREATE_TIME, " +
                        //"CREATOR, " +
                        //"LAST_MODIFIED, " +
                        //"LAST_MODIFIER, " +
                        "VSTATUS, " +
                        //"DESCRIPTION, " +
                        //"REFS, " +
                        "POST_ADDRESS," +
                        "POST_CODE," +
                        "LINKMAN, " +
                        "EMAIL," +
                        "MOBILE," +
                        "PHONE," +
                        "FAX," +
                        "REGION," +
                        "FOUND_YEAR," +
                        "SCALE," +
                        "PARENT_ID) " +
                        "VALUES (" +
                        "$COMPANY.getId()," +
                        "$COMPANY.getName()," +
                        //"$COMPANY.getLocalName()," +
                        "$COMPANY.getPriority()," +
                        //"$COMPANY.getCreateTime()," +
                        //"$COMPANY.getCreator()," +
                        //"$COMPANY.getLastModified()," +
                        //"$COMPANY.getLastModifier()," +
                        "$COMPANY.getVstatus()," +
                        //"$COMPANY.getDescription()," +
                        //"$COMPANY.getRefs()," +
                        "$COMPANY.getPostAddress()," +
                        "$COMPANY.getPostCode()," +
                        "$COMPANY.getLinkman()," +
                        "$COMPANY.getEmail()," +
                        "$COMPANY.getMobile()," +
                        "$COMPANY.getPhone()," +
                        "$COMPANY.getFax()," +
                        "$COMPANY.getRegion()," +
                        "$COMPANY.getFoundYear()," +
                        "$COMPANY.getScale()," +
                        "$COMPANY.getParentId())",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = CompanyDAOBean.INSERT_COMPANY_BY_DEPARTMENT,
                query = "INSERT INTO T_SYS_COMPANY(" +
                        "ID," +
                        "NAME, " +
                        "PRIORITY, " +
                        "VSTATUS, " +
                        "PARENT_ID) " +
                        "VALUES (" +
                        "$COMPANY.getId()," +
                        "$COMPANY.getName()," +
                        "$COMPANY.getPriority()," +
                        "$COMPANY.getVstatus()," +
                        "$COMPANY.getParentId())",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = CompanyDAOBean.DELETE_COMPANY,
                query = "UPDATE T_SYS_COMPANY SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
                resultClass = Company.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = CompanyDAOBean.UPDATE_COMPANY,
                query = "UPDATE T_SYS_COMPANY" +
                        " SET NAME = " + "$COMPANY.getName()," +
                        " PRIORITY = " + "$COMPANY.getPriority()," +
                        " VSTATUS = " + "$COMPANY.getVstatus()," +
                        " POST_ADDRESS = " + "$COMPANY.getPostAddress()," +
                        " POST_CODE = " + "$COMPANY.getPostCode()," +
                        " LINKMAN = " + "$COMPANY.getLinkMan()," +
                        " EMAIL = " + "$COMPANY.getEmail()," +
                        " MOBILE = " + "$COMPANY.getMobile()," +
                        " PHONE = " + "$COMPANY.getPhone()," +
                        " FAX = " + "$COMPANY.getFax()," +
                        " REGION = " + "$COMPANY.getRegion()," +
                        " FOUND_YEAR = " + "$COMPANY.getFoundYear()," +
                        " SCALE = " + "$COMPANY.getScale()," +
                        " PARENT_ID = " + "$COMPANY.getParentId()" +
                        " WHERE ID=" + "$COMPANY.getId()",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = CompanyDAOBean.UPDATE_COMPANY_BY_DEPARTMENT,
                query = "UPDATE T_SYS_COMPANY" +
                        " SET NAME = " + "$COMPANY.getName()," +
                        " PRIORITY = " + "$COMPANY.getPriority()," +
                        " VSTATUS = " + "$COMPANY.getVstatus()" +
                        " WHERE ID=" + "$COMPANY.getId()",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        )

                }
)

public class CompanyDAOBean extends SuperDAO implements CompanyDAO {
    public final static String GET_COMPANY_BY_ID = "getCompanyById";
    public final static String GET_ALL_COMPANYS = "getAllCompanys";
    //public final static String GET_NODES_BY_ROOT_ID = "getNodesByModuleId";
    //public final static String GET_NODES_BY_PARENT_ID = "getChildNodes";
    public final static String INSERT_COMPANY = "insertCompany";
    public final static String DELETE_COMPANY = "deleteCompany";
    public final static String UPDATE_COMPANY = "updateCompany";
    public final static String UPDATE_COMPANY_BY_DEPARTMENT = "updateCompanyByDepartment";
    public final static String INSERT_COMPANY_BY_DEPARTMENT = "insertCompanyByDeparment";

    public void insertCompany(Company company) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("COMPANY", company);
        executeNamedNativeUpdate(INSERT_COMPANY, params);
    }
    /** sheshengdian Add
     * 增加部门时同时向单位表增加数据
     * @param company
     */
    public void insertCompanyByDeparment(Company company) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("COMPANY", company);
        executeNamedNativeUpdate(INSERT_COMPANY_BY_DEPARTMENT, params);
    }

    public int deleteCompany(long id) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("ID", id);
        return executeNamedNativeUpdate(CompanyDAOBean.DELETE_COMPANY, params);
    }

    public int updateCompany(Company company) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("COMPANY", company);
        return executeNamedNativeUpdate(UPDATE_COMPANY, params);
    }
    public int updateCompanyByDepartment(Company company) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("COMPANY", company);
        return executeNamedNativeUpdate(UPDATE_COMPANY_BY_DEPARTMENT, params);
    }

    public Company getCompanyById(long id) {
        return (Company)getEntityObjectByColumn(CompanyDAOBean.GET_COMPANY_BY_ID, "ID", id);
    }

    public List<Company> getAllCompanys() {
        return (List<Company>)processNamedNativeQuery(CompanyDAOBean.GET_ALL_COMPANYS, Collections.EMPTY_MAP);
    }

}
